package com.zwf.service.impl;

import cn.hutool.cache.impl.CacheObj;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.zwf.dto.DocumentDTO;
import com.zwf.entity.CoreIndexEntity;
import com.zwf.entity.MemberEntity;
import com.zwf.entity.ProjectEntity;
import com.zwf.service.CoreIndexService;
import com.zwf.service.ExportService;
import com.zwf.service.MemberService;
import com.zwf.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ExportServiceImpl implements ExportService {

    @Resource
    private ProjectService projectService;

    @Resource
    private CoreIndexService coreIndexService;

    @Resource
    private MemberService memberService;

    @Override
    public DocumentDTO exportDoc(String projectId) throws Exception {
        // 填写数据
        Map<String, Object> dataMap = new HashMap<>();

        // 项目信息
        ProjectEntity projectEntity = projectService.getById(projectId);
        if (projectEntity == null) {
            throw new Exception("项目不存在");
        }
        dataMap.put("project", projectEntity);

        // 指标信息
        List<CoreIndexEntity> funcIndexList = coreIndexService.lambdaQuery()
                .eq(CoreIndexEntity::getBindProject, projectId)
                .list();
        dataMap.put("funcIndexList", funcIndexList);

        // 成员信息
        List<MemberEntity> memberList = memberService.lambdaQuery()
                .eq(MemberEntity::getBindProject, projectId)
                .list();
        dataMap.put("memberList", memberList);

        // 组装文件名
        String fileName = projectEntity.getProjectName() + ".docx";

        // 导出文件
        exportFile(dataMap, fileName);

        // 保存文件记录 --- 没有写数据库的操作了
        DocumentDTO documentDTO = new DocumentDTO();
        documentDTO.setDocumentId(IdUtil.simpleUUID());
        documentDTO.setDocumentPath("/upload/file/temp/" + fileName);
        documentDTO.setDocumentName(fileName);
        documentDTO.setDocumentType("docx");

        return documentDTO;
    }

    private void exportFile(Map<String, Object> dataMap, String fileName) throws Exception {
        try {
            // 临时文件
            String tempPath = "/upload/file/temp/" ;
            File tempFile = new File(tempPath + fileName);
            if (!tempFile.getParentFile().exists()) {
                boolean mkdirs = tempFile.getParentFile().mkdirs();
                if (!mkdirs) {
                    throw new Exception("创建临时文件夹失败");
                }
            }
            LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
            Configure configure = Configure.builder()
                    .bind("funcIndexList", policy)
                    .bind("memberList", policy)
                    .build();

            InputStream resourceAsStream = this.getClass().getResourceAsStream("/template/demo.docx");
            if (resourceAsStream == null) {
                throw new Exception("模板文件不存在");
            }

            XWPFTemplate template = XWPFTemplate.compile(resourceAsStream, configure).render(dataMap);
            template.writeToFile(tempPath + fileName);
            template.close();
        } catch (Exception e) {
            throw new Exception("导出文件失败");
        }
    }
}
